#include "SDL.h"
#include<stdlib.h>
#include<time.h>
#include "matrix.h"


const unsigned int FRAME_RATE = 1000 / 20;
//Events handler aux variable
SDL_Event event;
//Key-store variable
Uint8 *keystates;
//Keep the current frame
int frame = 0;


bool keycheck(){
    //If up is pressed
    if (keystates[SDLK_ESCAPE])
    {
        return true;
    }
    if( keystates[ SDLK_UP ] )
    {
        movimenta_player(0,-1);
        desenhar();
        movimenta_bixo();
        desenhar();
        return false;
    }

    //If down is pressed
    if( keystates[ SDLK_DOWN ] )
    {
        movimenta_player(0,1);
        desenhar();
        movimenta_bixo();
        desenhar();
        return false;
    }

    //If left is pressed
    if( keystates[ SDLK_LEFT ] )
    {
        movimenta_player(-1,0);
        desenhar();
        movimenta_bixo();
        desenhar();
        return false;
    }

    //If right is pressed
    if( keystates[ SDLK_RIGHT ] )
    {
        movimenta_player(1,0);
        desenhar();
        movimenta_bixo();
        desenhar();
        return false;
    }
    if( keystates[ SDLK_a ] ){
        player.ganhou=true;
        desenhar();
    return false;
    }
    if( keystates[ SDLK_s ] ){
        player.perdeu=true;
        desenhar();
    return false;
    }
    if( keystates[ SDLK_d ] ){
        player.key=true;
        desenhar();
    return false;
    }

    if( keystates[ SDLK_f ] ){
        inicializar();
        desenhar();
    return false;
    }
    if( keystates[ SDLK_SPACE ] ){
        player.inicio=false;
        desenhar();
    return false;
    }

    return false;
}


int main ( int argc, char** argv ){
    //Define if the user close the program
    bool done=false;
    //Initialize all SDL's Systems, load all images on surfaces and check for errors
    if (InitAll()==1)
        return 1;
    //Defines screen's parameters and check for errors
    screen = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen ){
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }
    //Build the first matrix
    inicializar();
    //Render the matrix for the first time
    desenhar();
    //Program Main Loop
    while (done==false) {
        //Start the frame rate cap
        unsigned int frameStart = SDL_GetTicks();
        //Test for exit events
        if( SDL_PollEvent( &event ) )        {
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                done = true;
            }
        }
        //Test each key pressed
        keystates = SDL_GetKeyState( NULL );
        //Key check returns True when Esc key is pressed
        done=keycheck();
        //Capping the frame
        unsigned int frameDuration = SDL_GetTicks() - frameStart;
        if(frameDuration < FRAME_RATE)
            SDL_Delay(FRAME_RATE - frameDuration);
    }
    printf("Exited cleanly\n");
    return 0;
}

